import { ref } from 'vue'
import axios from 'axios'
import { format } from 'date-fns'

export const opt = ref(0)

//用户ID
export const userId = ref('')
//用户名
export const userName = ref('')
//用户头像
export const userHeadShot = ref('')
//用户电话号码
export const userPhone = ref('')
//用户生日
export const userBirthday = ref('')
//用户性别
export const userGender = ref('')
//用户所有信息
export const user = JSON.parse(sessionStorage.getItem('user'))

import  img  from '@/assets/1.jpg'

//添加用户信息
if(user !== null){
    userId.value = user.id
    userName.value = user.name
    if(opt.value === 0)
         userHeadShot.value = "https://47.120.69.214:8787/"+user.headShotUrl
    else
        userHeadShot.value = img
    userPhone.value = user.phone
    if(user.birthday!== null){
        userBirthday.value = format(user.birthday, 'yyyy-MM-dd')
    }
    userGender.value = user.gender
}

export function addUserInfo() {
    const user = JSON.parse(sessionStorage.getItem('user'))
    userId.value = user.id
    userName.value = user.name
    if(opt.value === 0)
        userHeadShot.value = "https://47.120.69.214:8787/"+user.headShotUrl
    else
        userHeadShot.value = img
    userPhone.value = user.phone
    if(user.birthday!== null){
        userBirthday.value = format(user.birthday, 'yyyy-MM-dd')
    }
    userGender.value = user.gender
}

//好友列表
export const friendList = ref([])

export const groupUserList = ref([])
//自己
export const me = ref()
//获取好友列表
export function getFriendList(){
    axios({
            method : 'get',
            params : {
                "userId" : userId.value
            },
            url : 'https://47.120.69.214:8787/friend/getAllFriendOf'
        }).then(response => {
            friendList.value = response.data
            for(let i = 0; i < friendList.value.length; i++){
                if(friendList.value[i].id === userId.value){
                    me.value = friendList.value[i]
                    friendList.value.splice(i, 1)
                }
            }
            // console.log(me.value)
            groupUserList.value = [me.value]
        })
        .catch(error => console.log(error))
}

//类别
export const type = ref(0)

//用户信息更新
export function userInfoUpdate(){
    axios({
        method : 'get',
        url : 'https://47.120.69.214:8787/user/getById',
        params : {
            "userId" : userId.value
        }
    }).then(response => {
        sessionStorage.setItem('user', JSON.stringify(response.data))
        addUserInfo()
    })
    .catch(error => console.log(error))
}


//修改
export function change(list){
    // console.log(list)
    if(opt.value !== 0){
    for(let i=0;i<list.length;i++){
            list[i].name = '傻逼肖天琛'
        }
    }
}